package org.i_plus.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.model.BlogBean;
import org.i_plus.model.Blog_ReplyBean;
import org.i_plus.model.dao.Blog_ReplyHibernateDAO;
import org.i_plus.model.services.BlogService;
import org.i_plus.model.services.MemberService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;

public class showReplyServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private MemberService service;
	private BlogService blogService;
	private Blog_ReplyHibernateDAO blogReplyDAO;
	@Override
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (MemberService) context.getBean("MemberService");
		this.blogService = (BlogService) context.getBean("blogService");
		this.blogReplyDAO = (Blog_ReplyHibernateDAO)context.getBean("Blog_ReplyDAO");
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	    String account=request.getParameter("account");
//	    System.out.println("account:"+account);
	    String button = request.getParameter("prodaction");
//	    System.out.println("button:"+button);
	    String bnumtemp=request.getParameter("id");
//	    System.out.println("bnumtemp:"+bnumtemp);
	    
//	    WebApplicationContext context =
//     			WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
//		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		
		int bnum=0;
		if(bnumtemp!=null){
		bnum = Integer.parseInt(bnumtemp);
		}
		BlogBean blog=new BlogBean();
		blog.setB_num(bnum);
			
			if(button!=null && button.equals("reply")){
				BlogBean feedback = blogService.selectB(blog);
				System.out.println("feedback:"+feedback);
				String time = feedback.getB_time().toString();
				List<Blog_ReplyBean> blogreply=blogReplyDAO.selectByBNum(bnum);

				List<String> lists = new ArrayList<String>();
				lists.add(feedback.getContent());
				lists.add(time);
				lists.add(bnumtemp);
				for (Blog_ReplyBean list : blogreply){
					lists.add(list.getMemberBean().getAccount());
					lists.add(list.getRe_B_content());
					
					
				}


				Gson gson = new Gson();
				String result = gson.toJson(lists);
				
				
				
				
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(result);
//				System.out.println("result: " +result);
				return;	
			}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}

	
}
